package com.zhou.framework.filter;

import com.zhou.framework.config.DbSettingConfig;
import com.zhou.framework.constant.CacheKeyEnum;
import com.zhou.framework.redis.util.RedisUtil;
import com.zhou.util.StringTool;
import com.zhou.org.dao.Setting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 注册过滤器
 * @author lang.zhou
 * @date 2022/5/6 15:04
 */
@Slf4j
@Configuration
public class FilterRegister {
    @Bean
    public FilterRegistrationBean<CrossOriginFilter> corsFilterRegistration(DbSettingConfig config) {
        FilterRegistrationBean<CrossOriginFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new CrossOriginFilter());
        registration.addUrlPatterns("/*");
        registration.setName("crossOriginFilter");
        //order越小，优先级越高
        registration.setOrder(100);
        Map<String, String> initParameters = new HashMap<>(0);
        registration.setInitParameters(initParameters);
        return registration;
    }
    @Bean
    public FilterRegistrationBean<BlackListFilter> blackListFilterRegistration() {
        FilterRegistrationBean<BlackListFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);

        registration.setFilter(new BlackListFilter());
        registration.addUrlPatterns("/*");
        registration.setName("blackListFilter");
        //order越小，优先级越高
        registration.setOrder(1);
        Map<String, String> initParameters = new HashMap<>(0);
        registration.setInitParameters(initParameters);
        return registration;
    }
    @Bean
    public FilterRegistrationBean<CommonFilter> commonFilterRegistration() {
        FilterRegistrationBean<CommonFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);

        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/*");
        registration.setName("commonFilter");
        registration.setOrder(99);
        Map<String, String> initParameters = new HashMap<>(0);
        registration.setInitParameters(initParameters);
        return registration;
    }
    @Bean
    public FilterRegistrationBean<XssFilter> xssFilterRegistration(DbSettingConfig config, RedisUtil redisUtil) {
        Setting xss = config.getSetting("XSS", "url-patterns");
        String pat = xss == null ? null : xss.getValue();
        String urlPatterns = StringTool.safeToString(pat,"/*");

        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
        //
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        //过滤器类（继承Filter）
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns(urlPatterns.split(","));
        registration.setName("xssFilter");
        Setting s2 = config.getSetting("XSS", "excludes");

        String ec = s2 == null ? null : s2.getValue();
        //排除链接
        String excludes = StringTool.safeToString(ec,"/message/send,/activiti-explorer/,/module/saveModule,/message/messageTemplate/saveTemplate,/system/push/notice/saveNotice,/buss/ques/saveQues,/system/words/index,/word/app/html2Word,/buss/ques/myExam/submitExam,/buss/ques/addQuesVerify,/buss/ques/changeQuesVerify");
        List<Pattern> patterns = getPattern(excludes);
        redisUtil.set(CacheKeyEnum.XSS_URL,patterns);
        log.info("xss url["+excludes+"]");
        registration.setOrder(98);
        Map<String, String> initParameters = new HashMap<>();

        registration.setInitParameters(initParameters);
        return registration;
    }
    public List<Pattern> getPattern(String urls){
        List<Pattern> excludes = null;
        if (StringTool.isNotEmpty(urls)){
            String[] url = urls.split(",");
            excludes = new ArrayList<>(url.length);
            if(url.length > 0){
                for(String s : url){
                    excludes.add(Pattern.compile("^" + s.trim()));
                }
            }
        }
        return excludes;
    }
}
